﻿namespace MM.Common
{
    using System;
    using System.IO;

    public class clsLogTool
    {
        private bool _isActive = true;
        private bool _isLogTime = true;
        private string strLogFileName;
        private string strLogFilePath;

        private long GetFileLenth(string logfilename)
        {
            FileInfo info = new FileInfo(logfilename);
            return info.Length;
        }

        private string Rename4Backup(string strlogfilename)
        {
            return (strlogfilename.Substring(0, strlogfilename.LastIndexOf('.')) + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log");
        }

        public void WriteLog(string log)
        {
            if (this._isActive)
            {
                StreamWriter writer = null;
                if (!File.Exists(this.strLogFilePath + this.strLogFileName))
                {
                    writer = File.CreateText(this.strLogFilePath + this.strLogFileName);
                }
                else if (File.Exists(this.strLogFilePath + this.strLogFileName))
                {
                    writer = File.AppendText(this.strLogFilePath + this.strLogFileName);
                }
                if (this._isLogTime)
                {
                    writer.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "-->" + log);
                }
                else
                {
                    writer.WriteLine(log);
                }
                writer.Close();
                writer = null;
            }
        }
        public void WriteLogCharge(string log)
        {

            StreamWriter writer = null;
            if (!File.Exists(this.strLogFilePath + this.strLogFileName))
            {
                writer = File.CreateText(this.strLogFilePath + this.strLogFileName);
            }
            else if (File.Exists(this.strLogFilePath + this.strLogFileName))
            {
                writer = File.AppendText(this.strLogFilePath + this.strLogFileName);
            }
            writer.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "-" + log);
            writer.Close();
            writer = null;
        }

        public bool isActive
        {
            get
            {
                return this._isActive;
            }
            set
            {
                this._isActive = value;
            }
        }

        public bool isLogTime
        {
            get
            {
                return this._isLogTime;
            }
            set
            {
                this._isLogTime = value;
            }
        }

        public string LogFileName
        {
            get
            {
                return this.strLogFileName;
            }
            set
            {
                this.strLogFileName = value;
            }
        }

        public string LogFilePath
        {
            get
            {
                return this.strLogFilePath;
            }
            set
            {
                this.strLogFilePath = value;
                if (!Directory.Exists(this.strLogFilePath))
                {
                    Directory.CreateDirectory(this.strLogFilePath);
                }
            }
        }
    }

    public static class LogHelp
    {
        private static clsLogTool _log = new clsLogTool();

        static LogHelp()
        {
            _log.LogFilePath = @"D:\LOG\";
            _log.LogFileName = "ErrorLog.txt";
        }

        public static void WriteLogInfo(string strError)
        {
            _log.LogFilePath = @"D:\LOG\";
            _log.LogFileName = "ErrorLog.txt";
            _log.WriteLog(strError);
        }
        /// <summary>
        /// 记录每一次sim请求
        /// </summary>
        /// <param name="strLogFilePath"></param>
        /// <param name="LogInfo"></param>
        public static void WriteLogInfo(string strLogFilePath, string strLogFileName, string LogInfo)
        {
            _log.LogFilePath = strLogFilePath;
            _log.LogFileName = strLogFileName + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            _log.WriteLogCharge(LogInfo);
        }
    }
}

